package top.malaoshi.web.utils;

import top.malaoshi.util.ConfigFileUtil;
import top.malaoshi.util.Out;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class ConfigReader {

    public static final String CLASSPATH = "classpath:";
    public static final String WEB_INF = "web-inf:";
    private String path;
    private File pathAll = null;
    public ConfigReader(String path){
        this.path = path;
    }

    public Map<String,String> read() throws IOException {
        if(path.toLowerCase().startsWith(CLASSPATH)){
            String classpath = PathUtils.getClassPath().getAbsolutePath();
            pathAll = new File(classpath , path.substring(CLASSPATH.length()));
        }else if(path.toLowerCase().startsWith(WEB_INF)) {
            String webinf = PathUtils.getWebInfPath().getAbsolutePath();
            pathAll = new File(webinf , path.substring(WEB_INF.length()));
        }
        if(!pathAll.exists()){
            System.err.println(String.format("'%s'文件不存在", pathAll.getAbsolutePath()));
        }
        ConfigFileUtil cfu = new ConfigFileUtil();
        cfu.load(pathAll);

        return cfu.toMap();

    }


}
